<?php
App::uses('AppController', 'Controller');
/**
 * Championships Controller
 *
 * @property Championship $Championship
 */
class ChampionshipsController extends AppController {

	public $uses = array('Championship','Match','Team');
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Championship->recursive = 0;
		$this->set('championships', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Championship->exists($id)) {
			throw new NotFoundException(__('Invalid championship'));
		}
		$options = array('conditions' => array('Championship.' . $this->Championship->primaryKey => $id));
		$this->set('championship', $this->Championship->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Championship->create();
			if ($this->Championship->save($this->request->data)) {
				$this->Session->setFlash(__('The championship has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The championship could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Championship->exists($id)) {
			throw new NotFoundException(__('Invalid championship'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Championship->save($this->request->data)) {
				$this->Session->setFlash(__('The championship has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The championship could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Championship.' . $this->Championship->primaryKey => $id));
			$this->request->data = $this->Championship->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Championship->id = $id;
		if (!$this->Championship->exists()) {
			throw new NotFoundException(__('Invalid championship'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Championship->delete()) {
			$this->Session->setFlash(__('Championship deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Championship was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	public function generateRoundOf16($k){
	
		$torneo = $this->Championship->findById($k);
		$entry = array();
		$entry['Match']['championship_id'] = $k;
		$entry['Match']['type'] = 'Octavos';
		$entry['Match']['ended'] = false;
		$entry['Match']['scoreA'] = 0;
		$entry['Match']['scoreB'] = 0;
		$entry['Match']['home'] = null;
		$entry['Match']['visitor'] = null;
		$partidos = array();
		$groups = $this->Team->find('all',array('group' => 'Team.groups'));
	
		for ($i = 0; $i < $torneo['Championship']['groups']; $i++){
		
			$entry['Match']['name'] = 'Ganador grupo '.$groups[$i]['Team']['groups'].' vs Segundo grupo '.$groups[$i+1]['Team']['groups'];
			array_push($partidos,$entry);
			$i++;
	
		}
	
		for ($i = 0; $i < $torneo['Championship']['groups']; $i++){
			$entry['Match']['name'] = 'Ganador grupo '.$groups[$i+1]['Team']['groups'].' vs Segundo grupo '.$groups[$i]['Team']['groups'];
			array_push($partidos,$entry);
			$i++;
		}
	
		if ($this->Match->saveMany($partidos)){
			return true;
		} else {
			return false;
		}
	}
	
	public function generateQuarter($k,$i){
	
		$entry = array();
		$entry['Match']['championship_id'] = $k;
		$entry['Match']['type'] = 'Cuartos';
		$entry['Match']['ended'] = false;
		$entry['Match']['scoreA'] = 0;
		$entry['Match']['scoreB'] = 0;
		$entry['Match']['home'] = null;
		$entry['Match']['visitor'] = null;
		$partidos = array();
		
		$entry['Match']['name'] = 'Ganador partido '.($i - 7).' vs Ganador partido '.($i - 6);
		array_push($partidos,$entry);
		$entry['Match']['name'] = 'Ganador partido '.($i - 5).' vs Ganador partido '.($i - 4);
		array_push($partidos,$entry);
		$entry['Match']['name'] = 'Ganador partido '.($i - 3).' vs Ganador partido '.($i - 2);
		array_push($partidos,$entry);
		$entry['Match']['name'] = 'Ganador partido '.($i - 1).' vs Ganador partido '.$i;
		array_push($partidos,$entry);
		
		if ($this->Match->saveMany($partidos)){
			return true;
		} else {
			return false;
		}
	
	
	}
	
	public function generateSemi($k,$i){
	
		$entry = array();
		$entry['Match']['championship_id'] = $k;
		$entry['Match']['type'] = 'Semifinal';
		$entry['Match']['ended'] = false;
		$entry['Match']['scoreA'] = 0;
		$entry['Match']['scoreB'] = 0;
		$entry['Match']['home'] = null;
		$entry['Match']['visitor'] = null;
		$partidos = array();
		
		$entry['Match']['name'] = 'Ganador partido '.($i - 3).' vs Ganador partido '.($i - 2);
		array_push($partidos,$entry);
		$entry['Match']['name'] = 'Ganador partido '.($i - 1).' vs Ganador partido '.$i;
		array_push($partidos,$entry);
		
		if ($this->Match->saveMany($partidos)){
			return true;
		} else {
			return false;
		}
	
	
	}
	
	public function generateThird($k,$i){
	
		$third = array();
		$third['Match']['championship_id'] = $k;
		$third['Match']['type'] = 'Tercer lugar';
		$third['Match']['ended'] = false;
		$third['Match']['scoreA'] = 0;
		$third['Match']['scoreB'] = 0;
		$third['Match']['home'] = null;
		$third['Match']['visitor'] = null;
		$third['Match']['name'] = 'Perdedor partido '.($i - 1).' vs Perdedor partido '.$i;
		$partidos = array();
		array_push($partidos,$third);
		
		if ($this->Match->saveMany($partidos)){
			return true;
		} else {
			return false;
		}
	}
	
	public function generateFinal($k,$i){
	
		$entry = array();
		$entry['Match']['championship_id'] = $k;
		$entry['Match']['type'] = 'Final';
		$entry['Match']['ended'] = false;
		$entry['Match']['scoreA'] = 0;
		$entry['Match']['scoreB'] = 0;
		$entry['Match']['home'] = null;
		$entry['Match']['visitor'] = null;
		$entry['Match']['name'] = 'Ganador partido '.($i - 1).' vs Ganador partido '.$i;
		$partidos = array();
		array_push($partidos,$entry);
		
		if ($this->Match->saveMany($partidos)){
			return true;
		} else {
			return false;
		}
	}
	
	public function generar($id = null) {
		if (!$this->Championship->exists($id)) {
			throw new NotFoundException(__('Invalid championship'));
		}
		$this->Championship->id = $id;
		$teams = $this->Team->find('all',array('order' => array('Team.groups ASC')));
		$torneo = $this->Championship->findById($id);
		$entry = array();
		$entry['Match']['championship_id'] = $id;
		$entry['Match']['type'] = 'Grupo';
		$entry['Match']['ended'] = false;
		$entry['Match']['scoreA'] = 0;
		$entry['Match']['scoreB'] = 0;
		$cont = 0;
		$partidos = array();
		
		for ($i = 0; $i < ($torneo['Championship']['groups']*$torneo['Championship']['teamsPerGroup']); $i++){
		
			$entry['Match']['home'] = $teams[$i]['Team']['id'];
			for ($j = $i+1; ($j % $torneo['Championship']['teamsPerGroup']) > 0; $j++){
			
				$entry['Match']['visitor'] = $teams[$j]['Team']['id'];
				$entry['Match']['name'] = $teams[$i]['Team']['abbreviation'].' vs '.$teams[$j]['Team']['abbreviation'];
				array_push($partidos,$entry);
				$cont++;
		}
		
		}
		
		//Generar partidos de octavos
		
		if ($this->Match->saveMany($partidos)){
			$this->Session->setFlash(__('The match has been saved'));
			//$this->set('matches',$partidos);
			//$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('The match could not be saved. Please, try again.'));
			//$this->set('matches',$partidos);
			$this->redirect(array('action' => 'index'));
		}
		
		$this->generateRoundOf16($torneo['Championship']['id']);
		$cont += 8;
		$this->generateQuarter($id,$cont);
		$cont += 4;
		$this->generateSemi($id,$cont);
		$cont += 2;
		
		if ($torneo['Championship']['hasThirdPlace'] = 1) {
			$this->generateThird($id,$cont);
		}
		
		$this->generateFinal($id,$cont);
				
		$this->redirect(array('controller' => 'matches', 'action' => 'index'));
	}
	
	
}
